package org.hefan.pet.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.hefan.basic.util.AjaxResult;
import org.hefan.basic.util.PageList;
import org.hefan.pet.domain.Pet;
import org.hefan.pet.domain.PetDetail;
import org.hefan.pet.query.PetQuery;
import org.hefan.pet.service.IPetDetailService;
import org.hefan.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "宠物接口",description = "宠物接口详细描述")
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @Autowired
    private IPetDetailService petDetailService;

    //获取一个对象：get
    @ApiOperation(value = "查询单个宠物",notes = "需要传递id")
    @GetMapping("/{id}")
    public Pet findById(@PathVariable("id")Long id){
        return petService.loadById(id);
    }


    //获取所有对象：get
    @ApiOperation(value = "查询所有",notes = "不需参数")
    @GetMapping
    public List<Pet> findAll(){
        return petService.loadAll();
    }


    //分页 + 高级：post - 直接写，后面发送异步请求时再说
    //请求数据包中：请求行 + 请求头 + 请求体【参数就在请求体中】
    @ApiOperation(value = "分页查询宠物信息",notes = "需要传递当前页和每页显示的条数")
    @PostMapping
    public PageList<Pet> findPage(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }

    /**
     * 删除宠物
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除宠物",notes = "通过id删除")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            petService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 添加或修改宠物
     * @param pet
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改宠物",notes = "有id则为修改,没有id则是添加")
    public AjaxResult saveOrUpdate(@RequestBody Pet pet) {
        try {
            if (pet.getId() == null) {
                petService.save(pet);
            } else {
                petService.update(pet);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 批量删除宠物
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除宠物",notes = "传需要批量删除的宠物id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            petService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    public PetDetail getDetail(@PathVariable("id") Long id) {
        // 前端传来的是pet的id
        return petDetailService.getByPetId(id);
    }

    @PostMapping("/onsale")
    @ApiOperation(value = "宠物上架接口",notes = "传入批量需要上架的宠物的id组成的数组")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，"+e.getMessage()).setSuccess(false);
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "宠物下架接口",notes = "传入批量需要上架的宠物的id组成的数组")
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        try {
            petService.offsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，" + e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 接 {id:this.pet.id}
     * @param
     * @return
     */
    @PostMapping("/adopt")
    @ApiOperation(value = "领养宠物接口",notes = "")
    public AjaxResult adopt(@RequestBody Map<String, Long> map, HttpServletRequest request) {
        try {
            petService.adopt(map.get("id"), request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("领取失败，" + e.getMessage()).setSuccess(false);
        }
    }
}
